package com.guaniu.voip.sip;

import android.content.Context;
import android.hardware.camera2.CameraManager;

import com.guaniu.voip.utils.AppConstant;
import com.guaniu.voip.utils.SPUtils;
import com.guaniu.voip.utils.Utils;

import net.gotev.sipservice.SipAccountData;
import net.gotev.sipservice.SipServiceCommand;

import org.pjsip.PjCameraInfo2;

public class SipManager {

    private static SipManager instance = new SipManager();
    public static int SIP_REG_ONLINE = 1;
    public static int SIP_REG_OFFLINE = 0;
    public static int SIP_CALL_IDLE = 0;
    public static int SIP_CALL_OUTGOING = 1;
    public final static int CALL_CONNECTED = 2;
    public final static int CALL_CONNECTING = 3;
    public final static int CALL_DISCONNECTED_FOR_USER_BUSY = 4;
    public final static int SIP_CALL_INCOMING = 5;
    public final static int CALL_DISCONNECTED = 6;
    private SipManager() {
    }

    public static SipManager getInstance() {
        return instance;
    }

    int regState = SIP_REG_OFFLINE;
    int callStatus = 0;
    String mAccount;
    String callId;
    boolean isVideo = false;

    public void setCallStatus(int value){
        callStatus = value;
    }
    public void setOutGoingVideo(boolean value){
        isVideo = value;
    }
    public boolean isOutGoingVideo(){
        return isVideo;
    }
    public int getCallStatus(){
        return callStatus;
    }
    public void setRegState(String account, int value){
        mAccount = account;
        regState = value;
    }

    public String getMyAccount(){
        String sip_number = SPUtils.getInstance().getString(AppConstant.SP_SIP_NUMBER).trim();
        return sip_number;
    }
    public String getCallId(){
        return callId;
    }
    public String getAccount(){
        return mAccount;
    }
    public int getRegState(){
        return regState;
    }

    public void saveNewAccount(Context context, String sip_account_new,
                               String sip_pass_new,
                               String sip_server_new,
                               int sip_server_port){
        if (Utils.isSpace(sip_account_new)
                ||Utils.isSpace(sip_server_new)
                ||Utils.isSpace(sip_pass_new)
        ) {
            return;
        }

        String sip_number = SPUtils.getInstance().getString(AppConstant.SP_SIP_NUMBER).trim();
        if (!Utils.isSpace(sip_number)) {
            String sip_server = SPUtils.getInstance().getString(AppConstant.SP_SIP_SERVER);
            String id = "sip:"+sip_number+"@"+sip_server;
            SipServiceCommand.removeAccount(context, id);
        }
        SPUtils.getInstance().put(AppConstant.SP_SIP_NUMBER, sip_account_new);
        SPUtils.getInstance().put(AppConstant.SP_SIP_PASSWORD, sip_pass_new);
        SPUtils.getInstance().put(AppConstant.SP_SIP_SERVER, sip_server_new);
        SPUtils.getInstance().put(AppConstant.SP_SIP_PORT, sip_server_port);

        SipAccountData sipAccountData = new SipAccountData();
        sipAccountData.setUsername(sip_account_new);
        sipAccountData.setPassword(sip_pass_new);
        sipAccountData.setRealm("*");
        sipAccountData.setServer(sip_server_new);
        sipAccountData.setHost(sip_server_new);
        sipAccountData.setPort(sip_server_port);
        SipServiceCommand.setAccount(context, sipAccountData);

    }

    public void initVoip(Context context){
        CameraManager cm = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
        PjCameraInfo2.SetCameraManager(cm);

        final String ACC_USERNAME    = "1012002";
        final String ACC_PASSWORD    = "1234";
        final String ACC_SERVERNAME    = "192.168.21.113";
        String sip_number = SPUtils.getInstance().getString(AppConstant.SP_SIP_NUMBER);
        if (Utils.isSpace(sip_number)) {
            return;
        }
        String sip_pass = SPUtils.getInstance().getString(AppConstant.SP_SIP_PASSWORD);
        if (Utils.isSpace(sip_pass)) {
            return;
        }
        String sip_server = SPUtils.getInstance().getString(AppConstant.SP_SIP_SERVER);
        if (Utils.isSpace(sip_server)) {
            return;
        }
        //voip
        SipAccountData sipAccountData = new SipAccountData();
        sipAccountData.setUsername(sip_number.trim());
        sipAccountData.setPassword(sip_pass.trim());
        sipAccountData.setRealm("*");
        sipAccountData.setServer(sip_server.trim());
        SipServiceCommand.setAccount(context, sipAccountData);
    }

}
